//---------------------------------------------------------------------
// File: MapExecutingStep.cs
// 
// Summary: 
//---------------------------------------------------------------------


using System.IO;
using System.Xml;
using BizUnit.BizUnitOM;
using BizUnit.Extensions.BizTalk.Helpers;

namespace BizUnit.Extensions.BizTalk
{
    /// <summary>
    /// The MapExecutingStep can be used to execute a map and test the output from it
    /// It can be used on a single file or on multiple files if required. 
    /// For instance if you need to transform valid & invalid files and check that
    /// the output was valid or empty, this can be done in one step instead of two 
    /// separate steps.
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step. The step 
    /// can perform a transformation using a BizTalk map and output that to a directory
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="BizUnit.Extensions.BizTalk.dll" typeName="BizUnit.Extensions.BizTalk.MapExecutingStep">
    ///		<Map assemblyPath="C:\Program Files\Microsoft BizTalk Server 2006\SDK\Samples\Messaging\CBRSample\bin\Development\CBRSample.dll" 
    ///         typeName="CBRSample.CBRInput2CANMap"/>
    ///		<SourceDirectory>.\Rec_03</SourceDirectory>
    ///		<SearchPattern>CBRInput_*.xml</SearchPattern>
    ///		<DestinationDirectory>.\Rec_04</DestinationDirectory>
    ///     <DestinationNamePrefix>CBRSample</DestinationNamePrefix>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>Map</term>
    ///			<description>This is the BizTalk map that you wish to execute</description>
    ///		</item>
    ///     <item>
    ///			<term>SourceDirectory</term>
    ///			<description>This is the directory to pick up the test file</description>
    ///		</item>
    ///		<item>
    ///			<term>Pattern</term>
    ///			<description>Pattern for file names to be transformed</description>
    ///		</item>
    ///		<item>
    ///			<term>DestinationDirectory</term>
    ///			<description>Output directory</description>
    ///		</item>
    ///     <item>
    ///			<term>DestinationNamePrefix</term>
    ///			<description>Prefix for output files. All output files will be saved as .xml</description>
    ///		</item>
    ///	</list>
    ///	</remarks>
    public class MapExecutingStep : ITestStep, ITestStepOM
    {
        private string sourcePath;
        private string pattern;
        private string destinationPath;
        private string destinationNamePrefix;
        private string mapAssemblyPath;
        private string mapTypeName;

        public string SourcePath
        {
            get
            {
                return sourcePath;
            }
            set
            {
                sourcePath = value;
            }
        }
        public string Pattern
        {
            get
            {
                return pattern;
            }
            set
            {
                pattern = value;
            }
        }
        public string DestinationPath
        {
            get
            {
                return destinationPath;
            }
            set
            {
                destinationPath = value;
            }
        }
        public string DestinationNamePrefix
        {
            get
            {
                return destinationNamePrefix;
            }
            set
            {
                destinationNamePrefix = value;
            }
        }
        public string MapAssemblyPath
        {
            get
            {
                return mapAssemblyPath;
            }
            set
            {
                mapAssemblyPath = value;
            }
        }
        public string MapTypeName
        {
            get
            {
                return mapTypeName;
            }
            set
            {
                mapTypeName = value;
            }
        }
        public void Execute(XmlNode testConfig, Context context)
        {
            SourcePath = context.ReadConfigAsString(testConfig, "SourceDirectory");
            Pattern = context.ReadConfigAsString(testConfig, "SearchPattern");
            DestinationPath = context.ReadConfigAsString(testConfig, "DestinationDirectory");
            DestinationNamePrefix = context.ReadConfigAsString(testConfig, "DestinationNamePrefix");
            MapAssemblyPath = context.ReadConfigAsString(testConfig, "Map/@assemblyPath");
            MapTypeName = context.ReadConfigAsString(testConfig, "Map/@typeName");

            Execute(context);


        }

        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);
            string[] filelist = Directory.GetFiles(SourcePath, Pattern);

            XmlDocument outputDocument = new XmlDocument();
            XmlDocument inputDocument = new XmlDocument();
            string outputString;
            string filePath;
            int i = 0;

            foreach (string file in filelist)
            {
                inputDocument.Load(file);
                if (MapAssemblyPath != null && MapAssemblyPath.Length != 0)
                {
                    outputString = MapHelper.TransformMessage(inputDocument.OuterXml, MapTypeName, MapAssemblyPath);
                }
                else
                {
                    outputString = MapHelper.TransformMessage(inputDocument.OuterXml, MapTypeName);
                }
                outputDocument.LoadXml(outputString);
                filePath = DestinationPath + "\\" + DestinationNamePrefix + i.ToString() + ".xml";
                outputDocument.Save(filePath);
                context.LogInfo("MapExecutingStep has transformed file: \"{0}\" output is saved in \"{1}\"", file, filePath);
                i++;
            }

        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(SourcePath, "SourcePath");
            ArgumentValidation.CheckForEmptyString(Pattern, "Pattern");
            ArgumentValidation.CheckForEmptyString(DestinationPath, "Destination Path");
            ArgumentValidation.CheckForEmptyString(DestinationNamePrefix, "Destination Name Prefix");
            ArgumentValidation.CheckForEmptyString(MapAssemblyPath, "Map Assembly Path");
            ArgumentValidation.CheckForEmptyString(MapTypeName, "Map Type Name");


        }

        #endregion
    }
}



